package com.example.weiwing.home;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.weiwing.R;
import com.example.weiwing.home.calendar.CalendarFragment;
import com.example.weiwing.home.dynamic.DynamicFragment;
import com.example.weiwing.home.message.MessageFragment;
import com.example.weiwing.home.person.PersonFragment;
import com.example.weiwing.home.study.StudyFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.List;

public class HomeActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private DynamicFragment dynamicFragment;
    private StudyFragment studyFragment;
    private CalendarFragment calendarFragment;
    private MessageFragment messageFragment;
    private PersonFragment personFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        //初始化四个控件
        bottomNavigationView = findViewById(R.id.buttonNavigationView);

        selectedFragment(0);
        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.dynamic) {
                    selectedFragment(0);
                    return true;
                } else if (item.getItemId() == R.id.study) {
                    selectedFragment(1);
                    return true;
                } else if (item.getItemId() == R.id.calendar) {
                    selectedFragment(2);
                    return true;
                } else if (item.getItemId() == R.id.message) {
                    selectedFragment(3);
                    return true;
                } else if (item.getItemId() == R.id.person) {
                    selectedFragment(4);
//                    personFragment.refresh(yUser);
                    return true;
                }
                return false;
            }
        });

    }

    // 这个方法用于选择展示哪一个界面，
    private void selectedFragment(int position) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);

        if (position == 0) {
            if (dynamicFragment == null) {
                dynamicFragment = new DynamicFragment();
                fragmentTransaction.add(R.id.content_main, dynamicFragment);
            } else {
                fragmentTransaction.show(dynamicFragment);
            }
        } else if (position == 1) {
            if (studyFragment == null) {
                studyFragment = new StudyFragment();
                fragmentTransaction.add(R.id.content_main, studyFragment);
            } else {
                fragmentTransaction.show(studyFragment);
            }
        } else if (position == 2) {
            if (calendarFragment == null) {
                calendarFragment = new CalendarFragment();
                fragmentTransaction.add(R.id.content_main, calendarFragment);
            } else {
                fragmentTransaction.show(calendarFragment);
            }
        } else if (position == 3) {
            if (messageFragment == null) {
                messageFragment = new MessageFragment();
                fragmentTransaction.add(R.id.content_main, messageFragment);
            } else {
                fragmentTransaction.show(messageFragment);
            }
        } else if (position == 4) {
            if (personFragment == null) {
                personFragment = new PersonFragment();
                fragmentTransaction.add(R.id.content_main, personFragment);
            } else {
                fragmentTransaction.show(personFragment);
            }
        }

        // 提交所选 fragment
        fragmentTransaction.commit();
    }

    //  隐藏所有显示的 fragment
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (dynamicFragment != null) {
            fragmentTransaction.hide(dynamicFragment);
        }

        if (studyFragment != null) {
            fragmentTransaction.hide(studyFragment);
        }

        if (calendarFragment != null) {
            fragmentTransaction.hide(calendarFragment);
        }

        if (messageFragment != null) {
            fragmentTransaction.hide(messageFragment);
        }

        if (personFragment != null) {
            fragmentTransaction.hide(personFragment);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

//        if(calendarFragment == null) {
//            return ;
//        }
        if(getSupportFragmentManager().getFragments() != null && getSupportFragmentManager().getFragments().size() > 0) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            for (Fragment fragment : fragments) {
                if(fragment != null) {
                    fragment.onActivityResult(requestCode  , resultCode , data);
                }
            }
        }




    }
}